package top.someapp.applib;

import android.app.Application;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

/**
 * activity的基类，提供了屏幕显示方式、权限请求的常用方法
 *
 * @author zwz
 * Created on 2019-06-05
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected static final String INTENT_EXTRA_FULLSCREEN = "FULL_SCREEN";
    protected static final String INTENT_EXTRA_HIDE_NAVIGATION = "HIDE_NAVIGATION";

    @SuppressWarnings("unchecked")
    protected <T extends AppContext> T getAppContext() {
        Application app = getApplication();
        if (app instanceof AppContext) {
            return (T) app;
        }
        return null;
    }

    /**
     * 全屏显示
     */
    protected void requestFullScreen() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 隐藏导航栏
     */
    protected void hideBottomNavigation() {
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        window.setAttributes(params);
    }

    /**
     * 设置横屏显示
     */
    protected void setScreenLandscape() {
        if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

    /**
     * 设置竖屏显示
     */
    protected void setScreenPortrait() {
        if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    protected void checkCanWriteSettings(boolean force) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (force || !Settings.System.canWrite(this)) {
                Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                intent.setData(Uri.parse("package:" + getPackageName()));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        AppContext appContext = getAppContext();
        if (appContext != null) {
            appContext.setCurrentActivity(this);
        }
    }

    @Override
    public void onRequestPermissionsResult(@CodeDefine int requestCode,
        @NonNull String[] permissions,
        @NonNull int[] grantResults) {
        if (requestCode == CodeDefine.REQUEST_PERMISSION) {
            for (int i = 0, len = permissions.length; i < len; i++) {
                onPermissionsGrant(permissions[i],
                    grantResults[i] == PackageManager.PERMISSION_GRANTED);
            }
        }
    }

    protected void onPermissionsGrant(@NonNull String permissions, boolean granted) {
        // 子类覆盖实现
    }
}
